﻿using Pdoxcl2Sharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using V3MainTool.Model;

namespace V3MainTool.UI
{
    /// <summary>
    /// UCGoods.xaml 的交互逻辑
    /// </summary>
    public partial class UCGoods : UserControl
    {
        string fold;
        GoodsFamily myFamily = new GoodsFamily();
        public UCGoods()
        {
            InitializeComponent();
            LocalHelper.InitGoodsCate();
            List<GoodsCate> ls = new List<GoodsCate>();
            ls.Add(new GoodsCate() { Name = "", DisplayName = "全部" });
            foreach (var item in LocalHelper.GoodsCate)
            {
                ls.Add(new GoodsCate() { Name = item.Key, DisplayName = item.Value });
            }
            cmbType.ItemsSource = ls;
            cmbType.DisplayMemberPath = "DisplayName";
            cmbType.SelectedIndex = 0;
            cmbType.SelectionChanged += CmbType_SelectionChanged;
        }
        private void CmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadSelectedList();
        }
        private void LoadSelectedList()
        {
            GoodsCate g = cmbType.SelectedItem as GoodsCate;
            if (g.Name == "")
            {
                dgv.ItemsSource = this.myFamily.MyList;
            }
            else
            {
                dgv.ItemsSource = this.myFamily.MyList.FindAll(p => p.category == g.Name);
            }
        }
        private void btnReload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                fold = MainWindow.Instance.GetFold();
                string mypath = fold + @"\common\goods\";
                LocalHelper.Goods = LocalHelper.GetDic(fold + @"\localization\simp_chinese\goods_l_simp_chinese.yml");
                using (FileStream fs = new FileStream(mypath + "00_goods.txt", FileMode.Open))
                {
                    myFamily = ParadoxParser.Parse(fs, new GoodsFamily());
                }
                LoadSelectedList();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void btnOpenFold_Click(object sender, RoutedEventArgs e)
        {
            fold = MainWindow.Instance.GetFold();
            System.Diagnostics.Process.Start("explorer.exe", fold + @"\common\goods\");
        }
    }
    /// <summary>
    /// 商品类别
    /// </summary>
    public class GoodsCate
    {
        public string Name { get; set; }
        public string DisplayName { get; set; }
    }
}
